package paketDB;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Context;

public class UserFunctions {
	private static String login_tag = "login"; //$NON-NLS-1$
	// URL of the PHP API
	private static String loginURL = "http://lbnl.eu/learn2crack_login_api/"; //$NON-NLS-1$
	private static String register_tag = "register"; //$NON-NLS-1$
	private static String registerURL = "http://lbnl.eu/learn2crack_login_api/"; //$NON-NLS-1$

	/**
	 * @uml.property name="jSONParser"
	 * @uml.associationEnd multiplicity="(1 1)"
	 *                     inverse="userFunctions:paketDB.JSONParser"
	 */
	private JSONParser parser = null;

	// constructor
	public UserFunctions() {
		parser = new JSONParser();
	}

	/**
	 * Function to Login
	 **/
	@SuppressWarnings("unchecked")
	public JSONObject loginUser(String email, String password) {
		// Building Parameters
		@SuppressWarnings("rawtypes")
		List params = new ArrayList();
		params.add(new BasicNameValuePair("tag", login_tag)); //$NON-NLS-1$
		params.add(new BasicNameValuePair("Email", email)); //$NON-NLS-1$
		params.add(new BasicNameValuePair("Password", password)); //$NON-NLS-1$
		JSONObject json = parser.getJSONFromUrl(loginURL, params);
		return json;
	}

	/**
	 * Function to logout user Resets the temporary data stored in SQLite
	 * Database
	 * */
	public boolean logoutUser(Context context) {
		DatabaseHandler db = new DatabaseHandler(context);
		db.resetTables();
		return true;
	}

	/**
	 * Function to Register
	 **/
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public JSONObject registerUser(String fname, String lname, String email,
			String password) {
		// Building Parameters
		List params = new ArrayList();
		params.add(new BasicNameValuePair("tag", register_tag)); //$NON-NLS-1$
		params.add(new BasicNameValuePair("Vorname", fname)); //$NON-NLS-1$
		params.add(new BasicNameValuePair("Nachname", lname)); //$NON-NLS-1$
		params.add(new BasicNameValuePair("Email", email)); //$NON-NLS-1$
		params.add(new BasicNameValuePair("Password", password)); //$NON-NLS-1$
		JSONObject json = parser.getJSONFromUrl(registerURL, params);
		return json;
	}
}